import eventBus from "./EventBus";

import {server} from "./server";
import {RequestOperate, ResponseOperate} from "./opearte";
import {
    channelLoginPacket,
    realNameAuthenticationPacket,
    registerPacket,
    simpleRequest,
    simpleShortRequest
} from "./packet/req/req.packet";
import {ChannelServer} from "./ChannelServer";

eventBus.on(RequestOperate.LOGIN,(data:any)=>{

    server.send(data)

})

let token="";

eventBus.on(ResponseOperate.LOGIN, (data: { register:boolean, status:boolean,isAdult:boolean,token:string })=>{

    if(!data.register){

        console.error("登录失败，账号或密码错误")

        let registerData = registerPacket("13772149840","123456");
        server.send(registerData)
        return;
    }
    if(data.status &&!data.isAdult){
        console.error("需要进行实名认证")
        server.send(realNameAuthenticationPacket("屈领康","610402199303102696"))
    }else if(data.status &&data.isAdult){

        console.info(data)
        token=data.token;
        server.send(simpleRequest(RequestOperate.LOAD_REGION))

    }

})
eventBus.on(ResponseOperate.SEND_VERIFICATION_CODE, (data:{type:ResponseOperate,code:number})=>{

    if(data.code==404){
        console.error("请发送短信验证码")
    }

})

eventBus.on(ResponseOperate.REAL_NAME_AUTHENTICATION, (data:{type:ResponseOperate,code:number})=>{
    if(data.code==200){
        console.info("完成实名认证")

    }else{
        console.error("需要进行实名认证")

    }
})

eventBus.on(ResponseOperate.LOAD_REGION,
    (data:{type:ResponseOperate,data:Array<any>})=>{
        console.info("加载大区成功")
         let server1 =data.data[0].servers[0];
        server.send(simpleShortRequest(RequestOperate.LOAD_CHANNEL,server1.id))
    }
    )

let  channelServer=  null;
eventBus.on(ResponseOperate.LOAD_CHANNEL,
  async   (data:{type:ResponseOperate,data:Array<any>})=>{
        console.info("加载频道成功")
        console.info(data.data)
        const currentChannel=data.data[0];
        channelServer= new ChannelServer(currentChannel.serverIp,currentChannel.port)

        await  channelServer.init();

        channelServer.send(channelLoginPacket(1,token))

    }
)

